from time import sleep

from qturboframe.lib.utils.by import By
from qturboframe.lib.report.log import logger
from qturboframe.lib.utils.check import CheckBasic
from qturboframe.lib.webdriver.exceptions import SeleniumError
from qturboframe.public_ui.base_element import BaseElement


class Switch(BaseElement, CheckBasic):
    '''开关操作'''
    def __init__(self, element_para={By.CSS_SELECTOR:".h-switch"}, father=None, timeout=5):
        super().__init__(element_para, father, timeout)

    def switch_open(self):
        '''
        打开开关

        :Example:
         - 例: switch.switch_open()
        '''
        if "h-switch-checked" not in self.element.get_attribute("class"):
            self.element.click()

    def switch_close(self):
        '''
        关闭开关

        :Example:
         - 例: switch.switch_close()
        '''
        if "h-switch-checked" in self.element.get_attribute("class"):
            self.element.click()

    def switch(self, status):
        '''
        根据status的状态关闭或者打开开花

        status: True为打开开关，False为关闭开关

        :Example:
         - 例: switch.switch()
        '''
        if status:
            self.switch_open()
        else:
            self.switch_close()
